package com.soilflow.info.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.soilflow.common.core.domain.entity.SysUser;
import com.soilflow.common.core.domain.entity.SysUserReception;
import com.soilflow.common.domain.ColorfulUser;
import com.soilflow.common.domain.SysArticle;
import com.soilflow.common.domain.SysCollect;
import com.soilflow.common.domain.SysLike;
import com.soilflow.common.domain.dto.SysColleectDto;
import com.soilflow.common.domain.vo.SysCollectVo;
import com.soilflow.common.excle.DuocaiCollectExcel;
import com.soilflow.common.excle.DuocaiLLikeExcell;
import com.soilflow.common.utils.DateUtils;
import com.soilflow.common.utils.SecurityUtils;
import com.soilflow.common.utils.StringUtils;
import com.soilflow.info.mapper.SysArticleMapper;
import com.soilflow.info.mapper.SysCollectMapper;
import com.soilflow.info.service.ISysArticleService;
import com.soilflow.info.service.ISysCollectService;
import com.soilflow.info.service.ISysLikeService;
import com.soilflow.system.service.IColorfulUserService;
import com.soilflow.system.service.ISysUserReceptionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author party
 * @date 2023-10-09
 */
@Service
public class SysCollectServiceImpl extends ServiceImpl<SysCollectMapper, SysCollect> implements ISysCollectService {
    @Resource
    private SysCollectMapper sysCollectMapper;
    @Autowired
    private ISysUserReceptionService userReceptionService;

    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public SysCollect selectSysCollectById(Long id)
    {
        return sysCollectMapper.selectSysCollectById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param sysCollect 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<SysCollect> selectSysCollectList(SysCollect sysCollect) {
        return sysCollectMapper.selectSysCollectList(sysCollect);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param 【请填写功能名称】
     * @return 结果
     */
    @Override
    public boolean insertSysCollect(SysColleectDto dto) {
        boolean staus = dto.getStaus();
        Long userId = dto.getUserId();
        SysUserReception userReception = userReceptionService.selectUserById(userId);
        if (staus) {
            //为true则是表示用户收藏了,这里进行修改状态，变为不显示收藏
            SysCollect sysCollect = new SysCollect();
            BeanUtils.copyProperties(dto,sysCollect);
            return this.updateById(sysCollect);
        } else {
            //false则表示用户没有收藏,点击这里做判断
            //查询数据库，如果没有这条数据在新增一条，有但状态为1则进行修改
            LambdaQueryWrapper<SysCollect> wrapper=new LambdaQueryWrapper<>();
            wrapper.eq(SysCollect::getCreateId,dto.getUserId());
            wrapper.eq(SysCollect::getLandId,dto.getLandId());
            SysCollect collect = this.getOne(wrapper);
            if (collect == null) {
                SysCollect sysCollect = new SysCollect();
                BeanUtils.copyProperties(dto, sysCollect);
                sysCollect.setCreateId(userId);
                sysCollect.setCreateTime(DateUtils.getNowDate());
                sysCollect.setCreateBy(userReception.getUserName());
                sysCollect.setCollectUserNickname(userReception.getNickName());
                return this.save(sysCollect);
            } else {
                SysCollect sysCollect = new SysCollect();
                BeanUtils.copyProperties(dto,sysCollect);
                return this.updateById(sysCollect);
            }

        }

    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param sysCollect 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateSysCollect(SysCollect sysCollect) {
        sysCollect.setUpdateTime(DateUtils.getNowDate());
        return sysCollectMapper.updateSysCollect(sysCollect);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSysCollectByIds(Long[] ids)
    {
        for (Long id : ids) {
            LambdaUpdateWrapper<SysCollect> wrapper=new LambdaUpdateWrapper<>();
            wrapper.eq(SysCollect::getId,id);
            wrapper.set(SysCollect::getDeleted,1);
            int update = sysCollectMapper.update(null, wrapper);
        }
        return 1;
//        return sysCollectMapper.deleteSysCollectByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSysCollectById(Long id)
    {
        return sysCollectMapper.deleteSysCollectById(id);
    }

    /**
     * 对文章按收藏次数进行排序
     * @param sort 根据sort进行判断，如果sort为1，则按照降序排列 为0则按升序排列
     * @return
     */
    @Override
    public List<SysArticle> sortCollect(String sort) {
//        List<SysArticle> articles=new ArrayList<>();
//        LambdaQueryWrapper<SysCollect> wrapper=new LambdaQueryWrapper<>();
//        SysArticle sysArticle = new SysArticle();
//        if (StringUtils.isEmpty(sort)) {
//            return sysArticleService.selectarticle(sysArticle);
//        }
//        if (sort.equals("1")) {
////            wrapper.orderByDesc(SysCollect::getViewCount);
//            List<SysCollect> collects = this.list(wrapper);
//            for (SysCollect collect : collects) {
//                SysArticle article = sysArticleService.getById(collect.getArticleId());
//                Long id = article.getId();
//                //获取收藏量
////                article.setViewCount(collect.getViewCount());
//                //获取点赞量
//                LambdaQueryWrapper<SysLike> lambdaQueryWrapper=new LambdaQueryWrapper<>();
//                lambdaQueryWrapper.eq(SysLike::getArticleId,id);
//                SysLike like = sysLikeService.getOne(lambdaQueryWrapper);
////                article.setLikeCount(like.getViewCount());
//                articles.add(article);
//            }
//            return articles;
//        } else if (sort.equals("0")) {
////            wrapper.orderByAsc(SysCollect::getViewCount);
//            List<SysCollect> collects = this.list(wrapper);
//            for (SysCollect collect : collects) {
//                SysArticle article = sysArticleService.getById(collect.getArticleId());
//                Long id = article.getId();
////                article.setViewCount(collect.getViewCount());
//                //获取点赞量
//                LambdaQueryWrapper<SysLike> lambdaQueryWrapper=new LambdaQueryWrapper<>();
//                lambdaQueryWrapper.eq(SysLike::getArticleId,id);
//                SysLike like = sysLikeService.getOne(lambdaQueryWrapper);
////                article.setLikeCount(like.getViewCount());
//                articles.add(article);
//            }
//            return articles;
//        }
//        return sysArticleService.selectarticle(sysArticle);
        return null;
    }

    @Override
    public void export(HttpServletResponse response) throws IOException {
        List<DuocaiCollectExcel> excelList = this.baseMapper.export();
        EasyExcel.write(response.getOutputStream(), DuocaiCollectExcel.class).sheet().doWrite(excelList);
    }

    @Override
    public SysCollectVo userCollect(String id, Long userId) {
        Long ids=Long.parseLong(id);
        LambdaQueryWrapper<SysCollect> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(SysCollect::getCreateId,userId);
        wrapper.eq(SysCollect::getLandId,ids);
        SysCollect collect = this.getOne(wrapper);
        System.out.println("collect = " + collect);
        SysCollectVo sysCollectVo = new SysCollectVo();
        if (collect != null) {
            if (collect.getStatus() == 0) {
                sysCollectVo.setStatus(true);
                sysCollectVo.setSysCollectId(collect.getId());
            } else if (collect.getStatus() == 1) {
                sysCollectVo.setStatus(false);
                sysCollectVo.setSysCollectId(collect.getId());
            }
            return sysCollectVo;
        } else {
            sysCollectVo.setStatus(false);
            sysCollectVo.setSysCollectId(null);
            return sysCollectVo;
        }

    }

    @Override
    public boolean removearticleId(String id) {
        Long ids = Long.parseLong(id);
        LambdaQueryWrapper<SysCollect> wrapper=new LambdaQueryWrapper<>();
//        wrapper.eq(SysCollect::getArticleId,ids);
        boolean remove = this.remove(wrapper);
        return remove;
    }


}
